//
//  RHEnergySaveCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/12.
//

#import "RHEnergySaveCell.h"
#import "RHEnergySaveItemCell.h"
@interface RHEnergySaveCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectView;

@property (nonatomic, strong) NSArray *itemArr;


@end
@implementation RHEnergySaveCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}
-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    //二氧化碳减排
    NSNumber *num1 = [NSNumber numberWithFloat:[dic[@"carbonDioxideEmissionReduction"]floatValue]];
    NSString *str1 = [num1 decimalPlacesWithRoundUpWithDigit:1];
    //二氧化硫减排
    NSNumber *num2 = [NSNumber numberWithFloat:[dic[@"sulfurDioxideEmissionReduction"]floatValue]];
    NSString *str2 = [num2 decimalPlacesWithRoundUpWithDigit:1];
    //等效种植量
    NSString *str3 = [NSString stringWithFormat:@"%ld",[dic[@"equivalentPlantingAmount"] integerValue]];
    //节约标准煤
    NSNumber *num4  = [NSNumber numberWithFloat:[dic[@"saveStandardCoal"]floatValue]];
    NSString *str4 = [num4 decimalPlacesWithRoundUpWithDigit:1];
    
    self.itemArr = @[@{CellTitle:@"节约标准煤".rh_localized,CellContent:str4,CellIcon:@"kwt_energySave01",@"unit":@"kg"},
                          @{CellTitle:@"CO2减排量".rh_localized,CellContent:str1,CellIcon:@"kwt_energySave02",@"unit":@"kg"},
                          @{CellTitle:@"等效植树量".rh_localized,CellContent:str3,CellIcon:@"kwt_energySave03",@"unit":@"棵".rh_localized}
                         ];
    [self.collectView reloadData];
}

-(void)setupUI
{
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius  = 8;
    bgView.layer.masksToBounds = YES;
    bgView.layer.borderColor = kCOLOR_HEX(@"#E3E6ED").CGColor;
    bgView.layer.borderWidth = 1;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.text = @"社会贡献".rh_localized;
    titleLab.textColor = kCOLOR_HEX(@"#171A1D");
    titleLab.textAlignment = NSTextAlignmentRight;
    titleLab.font = kBOLDSYSTEMFONT(16);
    [bgView addSubview:titleLab];
 
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    
    self.itemArr = @[@{CellTitle:@"节约标准煤".rh_localized,CellContent:@"--",CellIcon:@"energySave01",@"unit":@"t"},
                          @{CellTitle:@"CO2减排量".rh_localized,CellContent:@"--",CellIcon:@"energySave02",@"unit":@"kg"},
                          @{CellTitle:@"等效植树量".rh_localized,CellContent:@"--",CellIcon:@"energySave03",@"unit":@"棵".rh_localized}];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectView.backgroundColor = [UIColor whiteColor];
    _collectView.layer.cornerRadius = 8;
    _collectView.layer.masksToBounds = YES;
    _collectView.showsVerticalScrollIndicator = NO;
    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [_collectView registerClass:[RHEnergySaveItemCell class] forCellWithReuseIdentifier:@"RHEnergySaveItemCell"];
    [bgView addSubview:_collectView];
    
    CGFloat w = (kUI_WINDOW_W -30 -24 - 20)/3.0;
    
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLab.mas_bottom).offset(5);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.offset(w*1.2+24);
        make.bottom.mas_offset(0);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHEnergySaveItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHEnergySaveItemCell" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:self.itemArr[indexPath.item][CellIcon]];
    cell.titleLab.text = self.itemArr[indexPath.item][CellTitle];
    NSString *key = self.itemArr[indexPath.item][CellContent];
    
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6]};
    
    NSString *content = [NSString stringWithFormat:@"%@ %@",self.itemArr[indexPath.item][CellContent],self.itemArr[indexPath.item][@"unit"]];
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:self.itemArr[indexPath.item][@"unit"]]];
    
    cell.contentLab.attributedText = attri_content;
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (kUI_WINDOW_W -30 -24 - 20)/3.0-0.1;
    NSLog(@"-----> item size:%f",w);
    return CGSizeMake(w, w*1.2);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(12, 12, 12, 12);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 12;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

@end
